import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col} from 'antd';
import {DATETIME_FORMAT} from '../constants';
import {formatAmount, formatDateTime} from '../utils';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import styles from './style.css';
import routes from './routes';
const {Description} = DescriptionList;

export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
    };
    componentDidMount() {
        this.props.init();
    }
    handleUpdateClick = () => {
        this.props.history.push(routes.update.format(this.props.id));
    }
    render() {
        const detailData = this.props.detail || {};
        const {options} = detailData;
        const {formatMessage} = this.props.intl;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.update) && hasPermissions(options, PERMISSION.update))
            operations.push(<Button key="update" onClick={this.handleUpdateClick} size="small">{
                formatMessage({
                    id: 'detailPanel.btn.edit',
                    defaultMessage: '编辑'
                })
            }</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.outbound) && hasPermissions(options, PERMISSION.outbound))
            operations.push(<Button key="outbound" onClick={this.handleUpdateClick} size="small">{
                formatMessage({
                    id: 'detailPanel.btn.outbound',
                    defaultMessage: '出库'
                })
            }</Button>);
        return (
            <div className="detail">
                <Card loading={this.state.loading}>
                    <DescriptionList size="large" title={<Row><Col span={12}>{
                        formatMessage({
                            id: 'detailPanel.card.title',
                            defaultMessage: '备件出运通知书信息'
                        })
                    }</Col>
                    <Col className={`col-align-right ${styles.detail}`} span={12}>{operations}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.partInvoiceCode',
                            defaultMessage: '备件箱单发票编号',
                        })}>{this.props.detail.partInvoiceCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.invoiceFee',
                            defaultMessage: '报关金额',
                        })}>{formatAmount(this.props.detail.invoiceFee)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.carrierName',
                            defaultMessage: '货代名称',
                        })}>{this.props.detail.carrierName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isMagnetic',
                            defaultMessage: '是否磁检',
                        })}>{this.props.detail.isMagnetic ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isChemical',
                            defaultMessage: '是否化工鉴定',
                        })}>{this.props.detail.isChemical ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isPL',
                            defaultMessage: '是否有装箱单',
                        })}>{this.props.detail.isPL ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isLoading',
                            defaultMessage: '是否有装柜图',
                        })}>{this.props.detail.isLoading ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isWarehousingAndAddress',
                            defaultMessage: '是否有进仓通知与地址',
                        })}>{this.props.detail.isWarehousingAndAddress ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isExpressDocuments',
                            defaultMessage: '是否有快递面单',
                        })}>{this.props.detail.isExpressDocuments ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isRandomFiles',
                            defaultMessage: '是否有随机文件',
                        })}>{this.props.detail.isRandomFiles ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                    </DescriptionList>
                </Card>
                <Card>
                    <DescriptionList title={formatMessage({
                        id: 'detailPanel.table.details',
                        defaultMessage: '备件出运通知书清单'
                    })}>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isLoadingCabinet',
                            defaultMessage: '是否装柜',
                        })}>{this.props.detail.isLoadingCabinet ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.loadingTime',
                            defaultMessage: '装柜时间',
                        })}>{formatDateTime(this.props.detail.loadingTime, DATETIME_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.loadingQuantity',
                            defaultMessage: '装柜数量',
                        })}>{this.props.detail.loadingQuantity}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.shortContainerCount',
                            defaultMessage: '20GP*数量',
                        })}>{this.props.detail.shortContainerCount}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.standardContainerCount',
                            defaultMessage: '40GP*数量',
                        })}>{this.props.detail.standardContainerCount}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.highContainerCount',
                            defaultMessage: '40HQ*数量',
                        })}>{this.props.detail.highContainerCount}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isEnteringWarehouse',
                            defaultMessage: '是否进仓',
                        })}>{this.props.detail.isEnteringWarehouse ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.deliveryTime',
                            defaultMessage: '要求送仓时间',
                        })}>{formatDateTime(this.props.detail.deliveryTime, DATETIME_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.inboundCode',
                            defaultMessage: '进仓编号',
                        })}>{this.props.detail.inboundCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isExpress',
                            defaultMessage: '是否快递',
                        })}>{this.props.detail.isExpress ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.expressPickupTime',
                            defaultMessage: '快递取货时间',
                        })}>{formatDateTime(this.props.detail.expressPickupTime, DATETIME_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.detailCarrierCode',
                            defaultMessage: '货代编码',
                        })}>{this.props.detail.detailCarrierCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.detailCarrierName',
                            defaultMessage: '货代名称',
                        })}>{this.props.detail.detailCarrierName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.expressCode',
                            defaultMessage: '面单号',
                        })}>{this.props.detail.expressCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isSelfMention',
                            defaultMessage: '是否自提',
                        })}>{this.props.detail.isSelfMention ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.collectPickupTime',
                            defaultMessage: '自提取货时间',
                        })}>{formatDateTime(this.props.detail.collectPickupTime, DATETIME_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.consignee',
                            defaultMessage: '提货人',
                        })}>{this.props.detail.consignee}</Description>
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPanel.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {getDetail, tableSearch} from './actions';
import {injectIntl} from './intl';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);
const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    permissions: getPermissions(state),
    detail: getDetailData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
